package edu.vt.ece.rt.benchmark.vacation;

import java.util.ArrayList;
import java.util.Random;

import edu.vt.ece.rt.benchmark.bank.BankAccount;
import edu.vt.ece.rt.quorum.ClustersManager;
import edu.vt.ece.rt.quorum.Resource;
import edu.vt.ece.rt.quorum.util.Names;
import edu.vt.ece.rt.util.Logger.Logger;


public class Benchmark extends edu.vt.ece.rt.benchmark.Benchmark{

	private Random random = new Random(hashCode());
	int quantity = Integer.getInteger(Names.transactions)*(Integer.getInteger(Names.nodes));
	int totalCustomers;
	int queryNum = 5;
	int types = 3 ;
	
	@Override
	protected void createLocalObjects() {
		
		totalCustomers = localObjectsCount*(Integer.getInteger(Names.nodes));
		Random r = new Random();
		
		for (int i = 0; i < localObjectsCount; i++) {
			new Car("car-"+i, (((r.nextInt(localObjectsCount) % 5) * 10) + 50), quantity, 0);
			new Flight("flight-"+i, (((r.nextInt(localObjectsCount) % 5) * 10) + 50), quantity, 0);
			new Hotel("hotel-"+i, (((r.nextInt(localObjectsCount) % 5) * 10) + 50), quantity, 0);
			Logger.debug("travel objects with id -"+"car - "+i+" created");
		}

		for (int i = 0; i < totalCustomers; i++) {
			new Customer("customer-"+i);
			Logger.debug("customer with id -"+"customer-"+i+" created");
		}

	}


	@Override
	protected Object randomId() {
		return null;
	}



	@Override
	protected int getOperandsCount() {
		return 0;
	}

	@Override
	protected void readOperation(Object... ids) {
		
		Random r = new Random();
		int range =  localObjectsCount;
		int nodeId = ClustersManager.baseClusterId;
		int customerNum = (nodeId * range) + r.nextInt(range);
		
		String customerId = "customer-" + customerNum;
		Vacation.checkReservation(customerId);
	}

	@Override
	protected void writeOperation(Object... ids) {
		
		Random r = new Random();
		
		
		int range =  localObjectsCount;
		int nodeId = ClustersManager.baseClusterId;		
		int customerNum = (nodeId * range) + r.nextInt(range);
		String customerId = "customer-" + customerNum;
		
		ArrayList<String> objIds = new ArrayList<String>();
		for (int i = 0; i< queryNum; i++) {
			int type = r.nextInt(types);
			String travelItem;
			if (type == 0) {
				travelItem = "car-";
			} else if (type == 1) {
				travelItem = "flight-";
			} else {
				travelItem = "hotel-";
			} 
			
			int itemNum = r.nextInt(localObjectsCount);			
			String itemId = travelItem + itemNum;
			objIds.add(itemId);
		}
		
		if (r.nextInt(100) < 90) {
			Vacation.makeReservation(customerId, objIds, nesting);
		} else {
			Vacation.UpdateTravelItem(objIds);
		}

	}

	@Override
	protected void checkSanity() {
		Travel t = null;
		for (int i = 0; i< localObjectsCount; i++) {
			t = (Travel) Resource.getQObject("car-"+i);
			t.checkItem();
			t = (Travel) Resource.getQObject("flight-"+i);
			t.checkItem();
			t = (Travel) Resource.getQObject("hotel-"+i);
			t.checkItem();
		}
		
		for (int i = 0; i < totalCustomers; i++) {
			Customer c = (Customer) Resource.getQObject("customer-"+i);
			c.checkSanity();
		}
	}


	@Override
	protected String getLabel() {
		// TODO Auto-generated method stub
		return "list-qr";
	}
}
